﻿using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace ArtMath.Flag.Helper
{
    public static class HotKey
    {
        [StructLayout(LayoutKind.Sequential)]
        struct KeyboardHook
        {
            public int vkCode, scanCode, flags, time, dwExtraInfo;
        }
        public static event Action<Keys> KeyDownEvent, KeyUpEvent;
        delegate int HookProc(int nCode, int wParam, IntPtr lParam);
        static int hKeyboardHook = 0;
        const int WH_KEYBOARD_LL = 13, WM_KEYDOWN = 0x100, WM_KEYUP = 0x101, WM_SYSKEYDOWN = 0x104, WM_SYSKEYUP = 0x105;
        static HookProc KeyboardHookProcedure;
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        static extern bool UnhookWindowsHookEx(int idHook);
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam);
        [DllImport("kernel32.dll")]
        static extern int GetCurrentThreadId();
        [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
        static extern IntPtr GetModuleHandle(string name);
        [DllImport("user32")]
        static extern int ToAscii(int uVirtKey, int uScanCode, byte[] lpbKeyState, byte[] lpwTransKey, int fuState);
        [DllImport("user32")]
        static extern int GetKeyboardState(byte[] pbKeyState);
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        static extern short GetKeyState(int vKey);
        public static bool Start()
        {
            if (hKeyboardHook == 0)
            {
                KeyboardHookProcedure = new HookProc(KeyboardHookProc);
                hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure, GetModuleHandle(System.Diagnostics.Process.GetCurrentProcess().MainModule.ModuleName), 0);
                if (hKeyboardHook == 0)
                {
                    Stop();
                    return false;
                }
            }
            return true;
        }
        public static bool Stop()
        {
            if (hKeyboardHook != 0)
            {
                if (UnhookWindowsHookEx(hKeyboardHook))
                {
                    hKeyboardHook = 0;
                    return true;
                }
            }
            return false;
        }
        static int KeyboardHookProc(int nCode, int wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                KeyboardHook MyKeyboardHookStruct = (KeyboardHook)Marshal.PtrToStructure(lParam, typeof(KeyboardHook));
                if (KeyDownEvent != null && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN))
                {
                    KeyDownEvent((Keys)MyKeyboardHookStruct.vkCode);
                }
                if (KeyUpEvent != null && (wParam == WM_KEYUP || wParam == WM_SYSKEYUP))
                {
                    KeyUpEvent((Keys)MyKeyboardHookStruct.vkCode);
                }
            }
            return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
        }
    }
}
